package com.cw.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cw.common.Constants;
import com.cw.common.R;
import com.cw.config.JwtConfig;
import com.cw.entity.Dict;
import com.cw.entity.dto.DictDto;
import com.cw.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Nice_BernardShaw
 * @date 2023/2/26 10:44
 */
@RestController
@Slf4j
@RequestMapping("/dict")
public class DictController {
    @Resource
    private DictService dictService;
    @Resource
    private RedisTemplate redisTemplate;
    @GetMapping()
    public R getSideBarList(){
        List<Dict> dictList = (List<Dict>) this.redisTemplate.opsForValue().get("dict_list");
        if (!ObjectUtil.isNotEmpty(dictList)){
            LambdaQueryWrapper<Dict> eq = new LambdaQueryWrapper<Dict>().eq(Dict::getParentId, Constants.INTEGER_ZERO);
            List<Dict> list = this.dictService.list(eq);
            this.redisTemplate.opsForValue().set("dictList",list);
        }
        return R.success((this.redisTemplate.opsForValue().get("dictList")));
    }
    @GetMapping("/page")
    public R page(@RequestParam(name = "page") Integer page,
                  @RequestParam(name = "pageSize") Integer  pageSize,
                  HttpServletRequest request){
        if(!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        log.info("page:{},pageSize:{}",page,pageSize);
        IPage queryPage = this.dictService.queryPage(new Page(page,pageSize));
        return R.success(queryPage);
    }
    @PostMapping()
    public R page(@RequestBody DictDto dictDto,HttpServletRequest request){
        if(!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        Dict dict = new Dict();
        BeanUtil.copyProperties(dictDto,dict);
        boolean save = this.dictService.save(dict);
        return R.success(save);
    }
    @PutMapping()
    public R update(@RequestBody DictDto dictDto,HttpServletRequest request){
        if(!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        Dict dict = new Dict();
        BeanUtil.copyProperties(dictDto,dict);
        boolean save = this.dictService.updateById(dict);
        return R.success(save);
    }
    @DeleteMapping
    public  R delete(@RequestParam(name = "id") Long id,HttpServletRequest request){
        if (!JwtConfig.getCurrentId(request).equals(Constants.ADMIN_ID)){
            return R.error("请提升至管理员权限");
        }
        dictService.removeById(id);
        return R.success("字典信息删除成功");
    }
}
